<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Write A Post</title>
<link rel="stylesheet" href="admin.css" />
<script type="text/javascript" src="editor.js"></script>
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="post.php" class="breadcrumb">Write A Post</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
				<h2>Write A Post</h2>
			<?php
				$printform=true;
				//Fix categories
				if(!is_array($_POST['categories']) || count($_POST['categories'])==0)
					$_POST['categories']=array(0);
				
				//Do the right thing
				if(isset($_POST['posttitle'])) {
					//Fix post data
					$_POST['posttitle']=fetch::unescapePostVar($_POST['posttitle']);
					$_POST['postcontent']=fetch::unescapePostVar($_POST['postcontent']);
					if($_POST['posttitle']==='') {
						echo('<h3 class="red">Error</h3>
						Please enter a title.<br />');
					}
					else {
						//Publishing or drafting or attaching
						if(isset($_POST['publish'])||isset($_POST['draft'])||isset($_POST['attach'])) {
							$comments=$_POST['comments']==1?true:false;
							echo('
							<h3>Post Saved</h3>');
							if(isset($_POST['publish'])) {
								$id=$_p->make_post($_POST['posttitle'],$_POST['postcontent'],$comments,true,$_POST['categories']);
								echo('<a href="../index.php?post='.$id.'">View post</a>');
								//Rebuild the rss feed
								$rssFile='../'.RSS_PATH;
								$xml=$_p->rebuildRSS();
								$rssHandle=fopen($rssFile,'w+');
								fwrite($rssHandle,$xml);
								fclose($rssHandle);
							}
							else {
								$id=$_p->make_post($_POST['posttitle'],$_POST['postcontent'],$comments,false,$_POST['categories']);
								echo('<a href="manageposts.php?p='.$id.'&amp;edit">Continue Editing</a>');
							}
							echo(' | <a href="dashboard.php">Return to dashboard</a>');
							//Attachment system
							if(isset($_POST['attach'])) {
								echo('<br />Attaching...');
								//Add and check result
								if(($attachedFile=$_p->attach(basename($_FILES['file_upload']['name']),$_FILES['file_upload']['tmp_name'],'../'.UPLOAD_PATH))!==false)
									echo('<br />Attachment "'.$attachedFile.'" added.');
								else
									echo('<br />Attachment failed to be added.');
							}
							//Don't print the form
							$printform=false;
						}
						//Preview
						else {
							echo('
							<h3>Previewing Post</h3>'.
							$_p->parsePost($_POST['postcontent'],USE_GESHI_POSTS));
						}
					}
				}
				
				if($printform) {
				echo('
					<form enctype="multipart/form-data" action="post.php"  method="post">
						<h3>Title</h3>
						<input type="text" class="full" name="posttitle" value="'.htmlentities($_POST['posttitle'],ENT_QUOTES,"UTF-8").'"/>
						<h3>Content</h3>
						<div class="toolbar">
						<a href="#" title="Bold" onclick="addtag(\'postcontent\', \'B\')"><b>B</b></a>
						<a href="#" title="Italics" onclick="addtag(\'postcontent\', \'I\')"><i>I</i></a>
						<a href="#" title="Underline" onclick="addtag(\'postcontent\', \'U\')"><u>U</u></a>
						<a href="#" title="Strikethrough" onclick="addtag(\'postcontent\', \'S\')"><s>S</s></a>
						<a href="#" title="Superscript" onclick="addtag(\'postcontent\', \'SUP\')">x<sup>2</sup></a>
						<a href="#" title="Subscript" onclick="addtag(\'postcontent\', \'SUB\')">x<sub>2</sub></a>
						<a href="#" class="deform" title="Code" onclick="addtag(\'postcontent\', \'CODE\')">code</a>
						<a href="#" class="deform" title="Create A Link" onclick="addurl(\'postcontent\')">url</a>
						<a href="#" class="deform" title="Insert an Image" onclick="addtag(\'postcontent\', \'IMG\')">img</a>
						<a href="#" class="deform" title="Add ReadMore Tag" onclick="addtext(\'postcontent\', \'[readmore]\')">readmore</a>
						<a href="#" class="deform" title="Add Link to a Post" onclick="linkpost(\'postcontent\')">+post</a>
						<a href="#" class="deform" title="Add Link to a Page" onclick="linkpage(\'postcontent\')">+page</a>
						</div>
						<textarea class="full" rows="30" cols="60" name="postcontent" id="postcontent">'.htmlentities($_POST['postcontent'],ENT_QUOTES,"UTF-8").'</textarea>
						<h3>Add Attachments</h3>');
						//Attachment system searching
						if(isset($_POST['attachsearch'])) {
							$searchResults=$_p->search_attachments($_POST['attachment_search_text'],'../'.UPLOAD_PATH);
							if($searchResults!==false) {
								if(count($searchResults)>0) {
									foreach($searchResults as $result) {
										echo('<strong>'.$result.'</strong> &raquo; Add a <a href="javascript:addtext(\'postcontent\',\'[attach='.$result.']'.$result.'[/attach]\')">download link</a>');
										
										//If is image, then show the embed link
										$ext=substr(strrchr($result,'.'),1);
										if($ext=='jpg'||$ext=='gif'||$ext=='bmp'||$ext=='png')
											echo(' or <a href="javascript:addtext(\'postcontent\',\'[img]download.php?attach='.$result.'[/img]\')">embed as image</a>');
										
										echo(' into this post<br />');
									}
								}
								else {
									echo('No results were returned, try broadening your search');
								}
							}
							else {
								echo('There was a problem getting the list of attachments; perhaps you haven\'t uploaded any?');
							}
						}
				echo('
						<input type="text" id="attachment_search_text" name="attachment_search_text" value="Enter filename to search for..." onfocus="javascript:clearsearch();" />
						<br /><br />
						<input type="submit" name="attachsearch" value="Search" />
						<h3>Attachments</h3>
						<input type="file" name="file_upload" />
						<input type="hidden" name="MAX_FILE_SIZE" value="'.MAX_UPLOAD_BYTES.'" /><br />
						Maximum upload size: '.MAX_UPLOAD_BYTES.' bytes<br />
						<br />
						<input type="submit" name="attach" value="Attach and draft" />
						<h3>Allow Comments</h3>
						<input type="radio" name="comments" '.($_POST['comments']!==0?'checked="checked"':'').' value="1" /> Yes<br />
						<input type="radio" name="comments" '.($_POST['comments']===0?'checked="checked"':'').' value="0" /> No
						<h3>Categories</h3>');
					//Category selection
					$categories=$_p->get_categories();
					if(count($categories)==0)
						echo('No categories available');
					foreach($categories as $catID=>$catInfo) {
						echo('<input type="checkbox" name="categories[]" value="'.$catID.'"'.(array_search($catID,$_POST['categories'])!==false?' checked="checked"':'').' /> '.$catInfo['name'].'<br />');
					}
				echo('
						<h3>Action</h3>
						<input type="submit" name="publish" value="Publish" />
						<input type="submit" name="draft" value="Save as draft" />
						<input type="submit" name="preview" value="Preview" />
					</form>
				');
				}
			?>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>